package com.scalar.chiptrack.operations.discrepancyLotsNotifications.actionCommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.operations.discrepancyLotsNotifications.dao.DiscrepancyLotsNotificationsDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Murali Krishna Yengala
 * Date: Sep 13, 2005
 * Time: 3:36:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class DeleteDiscrepancyLotsCommand extends ChipTrackExtendedAction {
    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute( USER_KEY );
        String notificationType = request.getParameter("notificationType");
        notificationType = notificationType == null ? "" : notificationType.trim();
        String[] notificationIds = request.getParameterValues( notificationType );
        ArrayList discrepancyLotsList = null;
        ActionForward forward = new ActionForward(ConfigManager.getIndexPageByGroupName (user.getGroupName()));
        if( notificationIds != null )
        {
            try
            {
                discrepancyLotsList = DiscrepancyLotsNotificationsDAO.loadSelectedDiscrepancyLots( notificationIds );
                if( discrepancyLotsList != null && discrepancyLotsList.size() > 0)
                {
                    request.setAttribute("discrepancyLots", discrepancyLotsList );
                    errors.put ("Are you sure, you want to Delete", null);
                    forward = mapping.findForward("confirmDelete");
                }
            }
            catch( Exception e)
            {
                errors.put(ERROR_INTERNAL_SERVER_ERROR, e.getMessage());
            }
        }
        return forward;
    }
}
